package com.dhkj.web.utils;

import com.dhkj.web.bean.Result;

import org.json.JSONArray;

import java.lang.reflect.Type;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * 数据返回统一处理  参考https://www.jianshu.com/p/ff619fea7e22
 */
public abstract class UZipResultObserver<T> implements Observer<Result<String>> {
    private static final String TAG = "BaseObserver";

    public Type type;

    @Override
    public void onNext(Result<String> response) {
        //在这边对 基础数据 进行统一处理  举个例子：
        if (response.getCode() == 0) {
            String jiemi = response.getResult();
            String jsonStr = ZipUtils.gunzip(jiemi);
            System.out.println("解码前：" + jiemi.length());
            System.out.println("解码完成:" + jsonStr);
            T res = DataUtils.parseString2List(jsonStr, type);
            onSuccess(res);
        } else {
            onFailure(null, response.getMessage());
        }
    }

    @Override
    public void onError(Throwable e) {//服务器错误信息处理
        onFailure(e, RxExceptionUtil.exceptionHandler(e));
        e.printStackTrace();
    }

    @Override
    public void onComplete() {

    }

    @Override
    public void onSubscribe(Disposable d) {
    }

    public abstract void onSuccess(T result);

    public abstract void onFailure(Throwable e, String errorMsg);

}
